<?php

namespace App\Http\Resources\Trip\Orders;

use App\Http\Resources\Train\TrainOrder\TrainOrderResource;
use App\Trip\Enums\TripTypes;
use App\Trip\Orders\TripOrderState;
use App\Trip\Orders\TripOrderStateDesc;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use JsonSerializable;

class TransferResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $order_info                     = parent::toArray($request);
        $orders                         = $order_info['orders'] ?? [];
        $order_info['orders']           = TrainOrderResource::collection($orders);
        $order_info['order_state_desc'] = TripOrderStateDesc::transDescByOrderState(TripTypes::TRAIN, $order_info['order_state']);
        $order_info['order_tip']        = TripOrderStateDesc::transIntroByOrderState(TripTypes::TRAIN, $order_info['order_state']);
        if (isset($order_info['max_pay_at']) && $order_info['order_state'] == TripOrderState::ORDER_STATE_CHANGE_WAITING_PAY) {
            $pay_date                = Carbon::parse($order_info['max_pay_at'])->toDateString();
            $pay_time                = Carbon::parse($order_info['max_pay_at'])->format('H:i');
            $order_info['order_tip'] = Str::replace(['{DATE}', '{TIME}'], [$pay_date, $pay_time], $order_info['order_tip']);
        }
        return $order_info;
    }
}
